package edu.fdzc.big_event.service.impl;

import edu.fdzc.big_event.mapper.UserMapper;
import edu.fdzc.big_event.pojo.User;
import edu.fdzc.big_event.service.UserService;
import edu.fdzc.big_event.utils.Md5Util;
import edu.fdzc.big_event.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public void register(String username, String password) {
        //给密码加密
        String md5String = Md5Util.getMD5String(password);
        userMapper.register(username,md5String);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public void updateAvter(String avterUrl) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        userMapper.updateAvter(avterUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        String password = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(password,id);
    }
}
